package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.project.domain.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.LaborSubcontractorMapper;
import com.hngates.project.domain.LaborSubcontractor;
import com.hngates.project.service.ILaborSubcontractorService;
import com.hngates.common.core.text.Convert;

/**
 * 劳务分包单位Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-24
 */
@Service
public class LaborSubcontractorServiceImpl implements ILaborSubcontractorService {
    @Autowired
    private LaborSubcontractorMapper laborSubcontractorMapper;

    /**
     * 查询劳务分包单位
     *
     * @param id 劳务分包单位主键
     * @return 劳务分包单位
     */
    @Override
    public LaborSubcontractor selectLaborSubcontractorById(Long id) {
        return laborSubcontractorMapper.selectLaborSubcontractorById(id);
    }

    /**
     * 查询劳务分包单位列表
     *
     * @param laborSubcontractor 劳务分包单位
     * @return 劳务分包单位
     */
    @Override
    public List<LaborSubcontractor> selectLaborSubcontractorList(LaborSubcontractor laborSubcontractor) {
        return laborSubcontractorMapper.selectLaborSubcontractorList(laborSubcontractor);
    }

    /**
     * 新增劳务分包单位
     *
     * @param laborSubcontractor 劳务分包单位
     * @return 结果
     */
    @Override
    public int insertLaborSubcontractor(LaborSubcontractor laborSubcontractor) {
        laborSubcontractor.setCreateTime(DateUtils.getNowDate());
        return laborSubcontractorMapper.insertLaborSubcontractor(laborSubcontractor);
    }

    /**
     * 修改劳务分包单位
     *
     * @param laborSubcontractor 劳务分包单位
     * @return 结果
     */
    @Override
    public int updateLaborSubcontractor(LaborSubcontractor laborSubcontractor) {
        laborSubcontractor.setUpdateTime(DateUtils.getNowDate());
        return laborSubcontractorMapper.updateLaborSubcontractor(laborSubcontractor);
    }

    /**
     * 批量删除劳务分包单位
     *
     * @param ids 需要删除的劳务分包单位主键
     * @return 结果
     */
    @Override
    public int deleteLaborSubcontractorByIds(String ids) {
        return laborSubcontractorMapper.deleteLaborSubcontractorByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除劳务分包单位信息
     *
     * @param id 劳务分包单位主键
     * @return 结果
     */
    @Override
    public int deleteLaborSubcontractorById(Long id) {
        return laborSubcontractorMapper.deleteLaborSubcontractorById(id);
    }

    /**
     * @param laborSubcontractorList 劳务分包单位信息列表
     * @param updateSupport          是否支持更新 如果已经存在 则进行更新数据
     * @param operName               操作用户
     * @return
     */
    @Override
    public String importLaborSubcontractor(List<LaborSubcontractor> laborSubcontractorList, Boolean updateSupport, String operName) {
        if (Objects.isNull(laborSubcontractorList) || laborSubcontractorList.size() == 0) {
            throw new BusinessException("导入劳务分包单位信息不能为空");
        }

        StringBuilder importMsg = new StringBuilder();
        //循环Excel中的记录
        for (LaborSubcontractor laborSubcontractor : laborSubcontractorList) {
            try {
                //constructionUnit是循环中 当前的记录对象
                //根据当前的Excel中的对象去数据库中查 查询时 以Excel中名称作为参数去查 返回值是 与项目同名的记录转为数据库对象
                LaborSubcontractor laborSubcontractor1 = laborSubcontractorMapper.selectLaborSubcontractorBySubcontractorId(laborSubcontractor.getSubcontractorId());
                //判断数据库对象是否存在
                if (laborSubcontractor1 == null) {
                    //如果不存在就创建
                    laborSubcontractor1 = new LaborSubcontractor();
                    //给对象的字段名赋值
                    laborSubcontractor1.setSubcontractorId(laborSubcontractor.getSubcontractorId());
                    laborSubcontractor1.setPersonnel(laborSubcontractor.getPersonnel());
                    laborSubcontractor1.setSubcontractLeader(laborSubcontractor.getSubcontractLeader());
                    laborSubcontractor1.setPhone(laborSubcontractor.getPhone());
                    laborSubcontractor1.setNativePlace(laborSubcontractor.getNativePlace());

                    //赋值完成后 讲这个对象插入到数据库中
                    laborSubcontractorMapper.insertLaborSubcontractor(laborSubcontractor1);

                    importMsg.append("<br/>" + "劳务分包单位信息" + laborSubcontractor1.getSubcontractorId() + "导入成功");

                } else if (updateSupport) {
                    laborSubcontractor1.setUpdateBy(operName);
                    laborSubcontractor1.setPersonnel(laborSubcontractor.getPersonnel());
                    laborSubcontractor1.setSubcontractLeader(laborSubcontractor.getSubcontractLeader());
                    laborSubcontractor1.setPhone(laborSubcontractor.getPhone());
                    laborSubcontractor1.setNativePlace(laborSubcontractor.getNativePlace());

                    laborSubcontractorMapper.updateLaborSubcontractor(laborSubcontractor1);

                    importMsg.append("<br/>" + "劳务分包单位信息" + laborSubcontractor1.getSubcontractorId() + "更新成功");

                } else {

                    importMsg.append("<br/>" + "劳务分包单位信息" + laborSubcontractor1.getSubcontractorId() + "已存在");

                }
            } catch (Exception e) {
                importMsg.append("<br/>" + "劳务分包单位信息" + laborSubcontractor.getSubcontractorId() + "导入时异常");
            }
        }
        return importMsg.toString();
    }
}
